

function checkPhone(phone) {
    if(phone.search(/^1[3458]{1}[0-9]{9}$/) == -1)
        return false;
    else
        return true;
}

function checkUsername(value) {
    if(value.search(/(?!\d+$)[\dA-Za-z\u4e00-\u9fa5]{4,16}/) == -1)
        return false;
    else
        return true;
}

var userCenter = {
    getData: function() {
        $.ajax({
            url: window.domain + window.requestUrl.userUrl,
            type: 'get',
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            success: function(res) {
                if(typeof(res.not_login) != 'undefined') {
                    window.setCookie('go_back_link', window.location.href);
                    window.location.href = './login.html';
                    return;
                }
                if(res.status) {
                    var user = res.data.user;
                    $('[data-role="username"]').val(user.user);
                    $('[data-role="phone"]').val(user.mobile);
                    $('[data-role="qq"]').val(user.qq);
                }
            },
            error: function(e) {
            }
        })
    },
    submit: function() {
        $('[data-role="submit"]').on('click', function() {
            var username = $('[data-role="username"]').val();
            if(username == '' || !checkUsername(username)) {
                $('[data-role="username"]').next('.tips').html('长度需为4-16个字符,且不能为纯数字');
            } else {
                $('[data-role="username"]').next('.tips').html('');
            }

            var phone = $('[data-role="phone"]').val();
            if(phone == '' || !checkPhone(phone)) {
                $('[data-role="phone"]').next('.tips').html('请输入正确的手机号');
            } else {
                $('[data-role="phone"]').next('.tips').html('');
            }

            var qq = $('[data-role="qq"]').val();

            if(checkPhone(phone) && checkUsername(username)) {
                var obj;
                if(qq) {
                    obj = JSON.stringify({
                        mobile: phone,
                        qq: qq,
                        nickname: username
                    })
                } else {
                    obj = JSON.stringify({
                        mobile: phone,
                        nickname: username
                    })
                }
                $.ajax({
                    url: window.domain + window.requestUrl.updateUserUrl,
                    type: 'post',
                    xhrFields: {
                        withCredentials: true
                    },
                    crossDomain: true,
                    data: {
                        user: obj
                    },
                    success: function(res) {
                        if(typeof(res.not_login) != 'undefined') {
                            window.setCookie('go_back_link', window.location.href);
                            window.location.href = './login.html';
                            return;
                        }
                        if(res.status) {
                            iosOverlay({
                                text: '修改成功',
                                duration: 1000
                            });
                        } else {
                            iosOverlay({
                                text: res.msg,
                                duration: 1000
                            });
                        }
                    },
                    error: function(e) {
                    }
                })
            }
        })
    },
    init: function() {
        this.getData();
        this.submit();
    }
}

userCenter.init();
